package com.cndw.kungfu.model.sys;

import com.cndw.kungfu.comm.tencent.QQDC;
import com.cndw.kungfu.config.Constants;
import com.cndw.kungfu.domain.LogCost;
import com.cndw.kungfu.domain.LogGot;
import com.cndw.kungfu.domain.LogPlayer;
import com.cndw.kungfu.domain.Player;
import com.cndw.kungfu.ext.TimeProcesser;
import com.cndw.kungfu.model.Context;

public class LogServer {

	/**
	 * 记录角色日志
	 * 
	 * @param player
	 * @param objectId
	 * @param eventId
	 * @param params
	 */
	public static void recordLog(Player player, int objectId, int eventId, Object... params) {
		if (player == null || objectId == 0 || eventId == 0) {
			return;
		}
		LogPlayer logPlayer = new LogPlayer();
		logPlayer.setObjectId(objectId);
		logPlayer.setEventId(eventId);
		logPlayer.setPlayerId(player.getPlayerId());
		logPlayer.setLevel(player.getLevel());
		logPlayer.setExp(player.getExp());
		logPlayer.setGold(player.getGoldAll());
		logPlayer.setGoldTicke(player.getGoldTicke());
		logPlayer.setAsset(player.getAsset());
		logPlayer.setIp(player.getLastIp());
		logPlayer.setAddTime(TimeProcesser.getUnixTime());

		if (params != null && params.length > 0) {
			StringBuilder join = new StringBuilder();
			int length = params.length;
			for (int i = 0; i < length; i++) {
				join.append(params[i]);
				if (i < length - 1) {
					join.append("$@");
				}
			}

			logPlayer.setParams(join.toString());
		}
		logPlayer.syncDB();

	}

	/**
	 * 记录玩家消费记录
	 * 
	 * @param playerId
	 * @param stype
	 * @param gold
	 * @param goldTicke
	 * @param goodsId
	 * @param num
	 */
	public static void costLog(long playerId, int stype, int gold, int goldTicke,int vipScore, int goodsId, int num) {
		LogCost logCost = new LogCost();
		logCost.setPlayerId(playerId);
		logCost.setStype(stype);
		logCost.setGold(gold);
		logCost.setVipScore(vipScore);
		logCost.setGoldTicke(goldTicke);
		logCost.setGoodsId(goodsId);
		logCost.setNum(num);
		logCost.setAddTime(TimeProcesser.getUnixTime());		
		logCost.syncDB();

	}
	
	public static void costEventLog(long playerId,int gold,int eventId){
		LogCost logCost = new LogCost();
		logCost.setPlayerId(playerId);
		logCost.setStype(Constants.PAY_GOLD);
		logCost.setGold(gold);
		logCost.setGoldTicke(0);
		logCost.setVipScore(0);
		logCost.setGoodsId(0);
		logCost.setNum(0);
		logCost.setEventId(eventId);
		logCost.setAddTime(TimeProcesser.getUnixTime());		
		logCost.syncDB();	
		if (QQDC.getInstance().isOpenDC()) {//附带上报腾讯api
			Player player = Context.getPlayerServer().getByPlayerId(playerId);
			if (player != null) {
				QQDC.getInstance().cost(player, gold, eventId);
			}
		}
	}
	
	public static void gotEventLog(long playerId,int gold,int eventId){
		LogGot logGot = new LogGot();
		logGot.setPlayerId(playerId);
		logGot.setGold(gold);
		logGot.setEventId(eventId);
		logGot.setAddTime(TimeProcesser.getUnixTime());		
		logGot.syncDB();
	}
	
	
}
